package org.azmat.springcache.SQL.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.azmat.springcache.SQL.Entity.Book;
import org.azmat.springcache.SQL.Mapper.BookMapper;
import org.azmat.springcache.SQL.Service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;

    // 查询
    @Override
    public Book getById(Integer id) {
        return super.getById(id);
    }

    // 新增
    @Override
    public boolean BookSave(Book book) {
        return super.save(book);
    }

    // 删除
    @Override
    public boolean removeById(Integer id) {
        return super.removeById(id);
    }

    // 更新
    @Override
    public boolean updateById(Book book) {
        return bookMapper.updateById(book) > 0;
    }


    // 查看所有
    @Override
    public List<Book> list() {
        return super.list();
    }

    // 分页查询，
    @Override
    public IPage<Book> page(IPage<Book> page, String search) {
        // 创建查询条件
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();

        // 根据关键字查询书名、作者或类型
        if (search != null && !search.trim().isEmpty()) {
            queryWrapper.like("name", search)
                    .or()
                    .like("author", search)
                    .or()
                    .like("type", search);
        }

        // 使用 MyBatis-Plus 的分页方法
        IPage<Book> resultPage = this.page(page, queryWrapper);

        // 检查分页结果是否为空
        if (resultPage == null) {
            throw new RuntimeException("分页查询结果为null");
        }

        return resultPage;
    }


}
